package com.yunsc.shop.controller.shop;


import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.shop.utils.ClientInfoUtils;
import com.yunsc.store.dto.CouponParamsDTO;
import com.yunsc.store.entity.YunscCouponEntity;
import com.yunsc.store.entity.YunscUserCouponEntity;
import com.yunsc.store.service.YunscCouponService;
import com.yunsc.store.service.YunscUserCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-09 10:29
 */
@Api(tags = "优惠券接口管理")
@RestController
// @RequestMapping("coupon")
public class CouponController {

    @Resource
    private YunscCouponService couponService;
    @Resource
    private YunscUserCouponService userCouponService;
    @Resource
    private ClientInfoUtils clientInfoUtils;

    @ApiOperation(value = "/myCoupon/list", tags = "获取用户优惠券列表")
    @GetMapping("/myCoupon/list")
    public Result<?> queryUserCouponList(@RequestParam("dataType") String dataType, @RequestParam("page") Integer page){
        Integer userId = SecurityUtil.getCurrentUserId();
        PageResult<YunscUserCouponEntity> userCoupons = userCouponService.queryUserCouponList(dataType, page, userId);
        return Result.resultPageList(userCoupons);
    }


    @ApiOperation(value = "/coupon/receive", tags = "领取优惠券")
    @PostMapping("myCoupon/receive")
    public Result<?> receiveCoupon(@RequestBody @Validated(ValidationGroups.Update.class) CouponParamsDTO params){
        Integer currentUserId = SecurityUtil.getCurrentUserId();
        userCouponService.receiveCoupon(params.getCouponId(), currentUserId);
        return Result.ok("领取成功");
    }

    @ApiOperation(value = "/coupon/list", tags = "获取优惠券列表")
    @GetMapping("coupon/list")
    public Result<?> queryCouponList(){
        Integer currentStoreId = clientInfoUtils.getCurrentStoreId();
        Integer currentUserId = SecurityUtil.getCurrentUserId();
        List<YunscCouponEntity> coupons = couponService.queryCouponList(currentUserId, currentStoreId);
        return Result.resultList(coupons);
    }


}
